package com.hkrt.demo.zip;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * @author Surpass
 * @Package com.hkrt.demo.zip
 * @Description: 压缩包解压
 * @date 2020/10/16 10:50
 */
public class SingleZipUnpackThe {

    private static InputStream inputStream;
    private static ZipInputStream bi;
    private static OutputStream fileOutputStream;
    private static String zipPath = "C:\\Users\\Surpass\\Desktop\\Bypass_1.14.2\\Bypass\\使用须知.zip";

    public static void main(String[] args)  {
        try {
            ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"));
            String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\"));
            System.out.println(zipFileParentPath);
            //压缩包输入流
            inputStream = new FileInputStream(new File(zipPath));
            bi = new ZipInputStream(inputStream);

            ZipEntry entry = null;
            while ( (entry = bi.getNextEntry())!=null){
                //输出文件流   压缩包路径+文件
                fileOutputStream = new FileOutputStream(zipFileParentPath+"\\" + entry.getName());
                //写文件
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = bi.read(bytes))!=-1){
                    fileOutputStream.write(bytes,0,len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (bi!=null){
                    bi.closeEntry();
                    bi.close();
                }
                if (fileOutputStream!=null){
                    fileOutputStream.close();
                }
                if (inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void singleZipUnpackThe(String[] args)  {
        try {
            ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"));
            String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\"));
            System.out.println(zipFileParentPath);

            //获得压缩包内文件
            Enumeration<? extends ZipEntry> entries = zipFile.entries();

            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = entries.nextElement();
                //输出文件流   压缩包路径+文件
                fileOutputStream = new FileOutputStream(zipFileParentPath+"\\" + zipEntry.getName());
                //写文件
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = bi.read(bytes))!=-1){
                    fileOutputStream.write(bytes,0,len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (bi!=null){
                    bi.closeEntry();
                    bi.close();
                }
                if (fileOutputStream!=null){
                    fileOutputStream.close();
                }
                if (inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
